package com.shouxin.attendance.service;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import com.shouxin.attendance.activity.LoadActivity;

import org.apache.log4j.Logger;

import java.util.List;

/**
 * Created by Jiang on 2015/11/24.
 */
public class CheckRun implements Runnable {

	private static final Logger logger = Logger.getLogger(CheckRun.class);

    private static final String MY_PKG_NAME = "com.shouxin.attendance";

    private Context context;

    public CheckRun(Context context) {
        this.context = context;
    }

    @Override
    public void run() {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfos = manager.getRunningTasks(30);
        boolean isAppRunning = false;
        for (ActivityManager.RunningTaskInfo info : taskInfos) {
            if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
                isAppRunning = true;
	            logger.debug(info.topActivity.getPackageName() + " info.baseActivity.getPackageName()=" +
			            info.baseActivity.getPackageName());
                break;
            }
        }
        if (!isAppRunning) {
            Intent it = new Intent(context, LoadActivity.class);
            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(it);
        }
    }
}
